package com.tuanzi.loan.web.bean.businesssystem;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.businesssystem.BusinessBill;
import com.tuanzi.loan.business.entity.businesssystem.BusinessIncome;
import com.tuanzi.loan.business.entity.businesssystem.BusinessMain;
import com.tuanzi.loan.business.entity.businesssystem.BusinessPrepay;
import com.tuanzi.loan.business.service.businesssystem.BusinessBillService;
import com.tuanzi.loan.business.service.businesssystem.BusinessMainService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.businesssystem.BusinessBillRequest;
import com.tuanzi.loan.vo.response.businesssystem.BusinessBillResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.businesssystem.BusinessBillLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;


@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class BusinessBillBean extends JSFListingBean<BusinessBillRequest, BusinessBillResponse> {
    @Autowired
    private BusinessMainService settlementCompanyService;

    @Autowired
    private BusinessBillService settlementBillService;

    private List<BusinessMain> comList;

    private List<BusinessIncome> incomeList;

    private List<BusinessPrepay> prepayList;

    private List<BusinessMain> companyList;

    private BusinessBill businessBill;

    private BusinessMain businessMain;

    private Double allBillIncome=0d;

    private Double lastAmount;

    @PostConstruct
    public void init() throws Exception {
        companyList = settlementCompanyService.getCompanyList();
        businessBill = new BusinessBill();
        incomeList = Lists.newArrayList();
        prepayList = Lists.newArrayList();
        initLazyDataModel();
    }

    private void initLazyDataModel() throws Exception {
        $ = new BusinessBillRequest();
        lazyDataModel = new BusinessBillLazyDataModel($);
    }
    public void count(){
        allBillIncome = settlementBillService.getAllIncome($);
    }

    public void onReset(){
        try {
            initLazyDataModel();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void onDelete(Long id) {
        BusinessBill one = settlementBillService.findOne(id);
        if (one.getBillStatus()!=null&&one.getBillStatus()==true){
            addWarnMessage("不能删除已经签收的发票");
            return;
        }
        try {
            settlementBillService.delete(one);
            settlementCompanyService.billDelete(one);
        }catch(Exception e) {
            e.printStackTrace();
        }

        addSuccessfullyMessage();
    }

    public void onAdd() {
        businessBill = new BusinessBill();
    }

    public void onSelectProduct() {
        Long productId = businessBill.getCompanyId();
        if (productId == null) {
            businessMain = new BusinessMain();
            return;
        }
        businessMain = settlementCompanyService.findOne(productId);
    }

    public void onSave() {
        if (!validate()) return;
        //获取公司名称
        if (businessBill.getBillStatus()!=null&&businessBill.getBillStatus()==true){
            addWarnMessage("已经签收，不支持更改！");
            return;
        }
        settlementBillService.onSave(businessBill);
        execute("PF('adding').hide();");
        update("billForm");
    }

    public boolean validate() {
        if (businessBill.getCompanyId() == null){
            addErrorMessage("没有选择主体");
            return false;
        }
        if (businessBill.getBillAmount() == null){
            addErrorMessage("没有开票金额");
            return false;
        }
        return true;
    }

    public void onSee(Long id){
        businessBill = settlementBillService.findOne(id);
    }
    public void onStatus(Long id){
        BusinessBill one = settlementBillService.findOne(id);
        if (one.getBillStatus()!=null&&one.getBillStatus()==true){
            addWarnMessage("已经签收");
            return;
        }
        one.setBillStatus(true);
        settlementBillService.saveOrUpdate(one);

    }

    public void onChangeIncomeAmount() {
        lastAmount=businessMain.getPrepayAmount()-businessBill.getBillAmount();
        return;

    }


    @Override
    protected List<String> getDictionaryCodes() {
        return  Lists.newArrayList(SETTLEMENT_SPREAD_CHANNEL,SETTLEMENT_INCOME_TYPE,
                SETTLEMENT_SPREAD_MODE, COOPERATION_MODE, SETTLEMENT_CYCLE,
                SETTLEMENT_BILLING_CONTENT,SETTLEMENT_BILL_TYPE,SETTLEMENT_PAY_MODE,PAYMENT_MAIN);
    }
}
